/**
 * @description 积分/余额抵扣规则计算
 */
export default {
  data() {
    return {}
  },
  methods: {
    // 获取影片用券后价格
    getFilmDiscount() {
      if (this.preInfo.film) {
        return this.isNaNStr(this.preInfo.film.discount)
      }
      return 0
    },

    // 获取卖品用券后价格
    getGoodsDiscount() {
      if (this.preInfo.goods) {
        return this.isNaNStr(this.preInfo.goods.discount)
      }
      return 0
    },
    // 计算最大可用积分总积分
    caclMaxIntegral() {
      const maxIntegral = this.isNaNStr(this.accountInfo.maxIntegral) // 本日可用积分上限
      let nowMaxIntegral = this.$u.plus(this.caclMaxFilmIntegral(), this.caclMaxGoodsIntegral()) // 卖品积分 + 商品可用积分
      const ableIntegral = this.balance2Intergral(this.$u.plus(this.getGoodsDiscount(), this.getFilmDiscount())) // 需要花费的金额对于积分
      nowMaxIntegral = nowMaxIntegral > ableIntegral ? ableIntegral : nowMaxIntegral
      return maxIntegral > nowMaxIntegral ? nowMaxIntegral : maxIntegral
    },

    // 计算最大影片可用积分
    caclMaxFilmIntegral() {
      if (!this.accountInfo.integralFilm) {
        return 0
      }
      const filmDiscount = this.getFilmDiscount() // 需要付的影票钱
      const filmTotal = this.caclFilmTotal() // 影票钱总价格

      const integralFilm = this.accountInfo.integralFilm
      const eqlRatio = this.isNaNStr(this.accountInfo.eqlRatio)
      const payRatio = this.isNaNStr(integralFilm.payRatio)
      const maxIntegral = this.isNaNStr(integralFilm.maxIntegral)
      const integral = this.$u.times(filmTotal, eqlRatio, payRatio) // 整个影票可用的积分
      const needPayIntegral = this.$u.times(filmDiscount, eqlRatio) // 需要付的影票钱对应的积分
      const result = this.getMin([integral, maxIntegral, needPayIntegral])
      return Math.floor(result) // 向下取整
    },

    // 计算最大卖品可用积分
    caclMaxGoodsIntegral() {
      if (!this.accountInfo.integralGoods) {
        return 0
      }
      const goodsDiscount = this.getGoodsDiscount() // 需要付的卖品/商品钱
      const goodsTotal = this.caclGoodsTotal() // 卖品/商品总价
      const integralGoods = this.accountInfo.integralGoods
      const eqlRatio = this.isNaNStr(this.accountInfo.eqlRatio)
      const payRatio = this.isNaNStr(integralGoods.payRatio)
      const maxIntegral = this.isNaNStr(integralGoods.maxIntegral)
      const integral = this.$u.times(goodsTotal, eqlRatio, payRatio) // 整个 卖品/商品 可用的积分
      const needPayIntegral = this.$u.times(goodsDiscount, eqlRatio) // 需要付的影票钱对应的积分
      const result = this.getMin([integral, maxIntegral, needPayIntegral])
      return Math.floor(this.isNaNStr(result)) // 向下取整
    },

    // 计算最大可花积分
    calcMaxIntegralCut(flag = false) {
      if (!flag && !this.selfCheckList.includes("integral")) {
        return 0
      }
      const myIntegral = this.isNaNStr(this.accountInfo.integral) // 我的积分
      const allIntegral = this.caclMaxIntegral() // 最大可用积分
      const ableIntegral = myIntegral > allIntegral ? allIntegral : myIntegral // 可花费积分
      return ableIntegral
    },

    // 换算积分=》金额
    integral2balance(integral) {
      integral = this.isNaNStr(integral)
      const eqlRatio = this.isNaNStr(this.accountInfo.eqlRatio)
      const money = this.$u.divide(integral, eqlRatio)

      return money
    },

    balance2Intergral(balance) {
      balance = this.isNaNStr(balance)
      const eqlRatio = this.isNaNStr(this.accountInfo.eqlRatio)
      const money = this.$u.times(balance, eqlRatio)

      return money
    },

    // 计算积分抵扣金额
    caclIntegralCut() {
      const ableIntegral = this.calcMaxIntegralCut() // 可花费积分
      return this.integral2balance(ableIntegral)
    },

    // 计算余额抵扣（共花费余额）
    caclBalanceCut() {
      if (this.selfCheckList.includes("balance")) {
        // 勾选余额
        const maxBalance = this.$u.minus(this.preInfo.needPay, this.caclIntegralCut()) // 最大可用余额
        const myBalance = this.isNaNStr(this.accountInfo.balance) // 我的余额
        const ableBalance = maxBalance > myBalance ? myBalance : maxBalance // 可用余额
        return ableBalance
      }
      return 0
    },

    // 计算影片花费积分
    caclFilmIntegralCut() {
      const calcMaxIntegralCut = this.calcMaxIntegralCut() // 已花费积分
      const caclMaxFilmIntegral = this.caclMaxFilmIntegral() // 最大影票积分
      return calcMaxIntegralCut > caclMaxFilmIntegral ? caclMaxFilmIntegral : calcMaxIntegralCut
    },

    // 计算影片花费积分抵用的金额
    caclFilmIntegral2Money() {
      const caclFilmIntegralCut = this.caclFilmIntegralCut()
      return this.integral2balance(caclFilmIntegralCut)
    },

    // 计算卖品花费积分
    caclGoodsIntegralCut() {
      const calcMaxIntegralCut = this.$u.minus(this.calcMaxIntegralCut(), this.caclFilmIntegralCut()) // 已花费积分 (影片优先级大于卖品)
      const caclMaxGoodsIntegral = this.caclMaxGoodsIntegral() // 最大可用卖品积分
      return calcMaxIntegralCut > caclMaxGoodsIntegral ? caclMaxGoodsIntegral : calcMaxIntegralCut
    },

    // 计算卖品花费积分抵用的金额
    caclGoodsIntegral2Money() {
      const caclGoodsIntegralCut = this.caclGoodsIntegralCut()
      return this.integral2balance(caclGoodsIntegralCut)
    },

    // 计算影片花费多少余额
    caclFilmBalanceCut() {
      const caclBalanceCut = this.caclBalanceCut() // 花费的余额
      const caclFilmIntegral2Money = this.caclFilmIntegral2Money() // 花费积分抵用的金额
      const getFilmDiscount = this.getFilmDiscount() // 共需要支付的影片金额
      const maxFilmBalance = this.$u.minus(getFilmDiscount, caclFilmIntegral2Money) // 最大影片可用余额

      return maxFilmBalance > caclBalanceCut ? caclBalanceCut : maxFilmBalance
    },

    // 计算卖品花费多少余额
    caclGoodsBalanceCut() {
      const caclBalanceCut = this.$u.minus(this.caclBalanceCut(), this.caclFilmBalanceCut()) // 花费的余额  (影片优先级大于卖品)
      const caclGoodsIntegral2Money = this.caclGoodsIntegral2Money()
      const getGoodsDiscount = this.getGoodsDiscount() // 共需要支付的影片金额
      const maxGoodsBalance = this.$u.minus(getGoodsDiscount, caclGoodsIntegral2Money)
      return maxGoodsBalance > caclBalanceCut ? caclBalanceCut : maxGoodsBalance
    },
    // 计算影片原价
    caclFilmTotal() {
      if (this.preInfo.film) {
        const filmPrice = this.preInfo.film.filmPrice
        return this.isNaNStr(filmPrice)
      }
      return 0
    },

    // 计算商品原价
    caclGoodsTotal() {
      if (this.preInfo.goods) {
        const price = this.preInfo.goods.price
        return this.isNaNStr(price)
      }
      return 0
    },
    isNaNStr(v) {
      v = (v + "").replace(/\\,/g, "")
      v = +v
      if (isNaN(v)) {
        return 0
      }
      return v
    },
    getMin(array) {
      const arr = array.map((i) => this.isNaNStr(i))
      return Math.min(...arr)
    }
  }
}
